package com.linxi.wordslog

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
import com.linxi.wordslog.wordslogviewmodel.WordViewModel
import kotlinx.android.synthetic.main.activity_words.*
import kotlinx.android.synthetic.main.header.view.*


class WordsActivity : AppCompatActivity() {
    private lateinit var controller: NavController
    private lateinit var appBarConfiguration: AppBarConfiguration
    private val wordsViewModel by viewModels<WordViewModel> {
        Injection.provideWordViewModelFactory(this)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_words)
        setSupportActionBar(toolbar)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val decorView = window.decorView
            decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            window.statusBarColor = Color.TRANSPARENT

        }
//        toolbar.setNavigationIcon(R.drawable.ic_user_logo)
        controller = findNavController(R.id.fragment2)
        appBarConfiguration = AppBarConfiguration(controller.graph, drawerLayout)
        toolbar.setupWithNavController(controller, appBarConfiguration)
        navigationView.setupWithNavController(controller)


        wordsViewModel.apply {
            name.observe(this@WordsActivity, Observer {
                navigationView.menu.findItem(R.id.menu_name).title = "姓名: $it"
            })
            age.observe(this@WordsActivity, Observer {
                navigationView.menu.findItem(R.id.menu_age).title = "年龄: $it"
            })
            sex.observe(this@WordsActivity, Observer {
                when (it) {
                    1 -> {
                        navigationView.menu.findItem(R.id.menu_sex).title = "性别: 男"
                    }
                    0 -> {
                        navigationView.menu.findItem(R.id.menu_sex).title = "性别: 女"
                    }
                    else -> {
                        navigationView.menu.findItem(R.id.menu_sex).title = "性别: 保密"
                    }
                }
            })
            sign.observe(this@WordsActivity, Observer {
                val signTextView = navigationView.getHeaderView(0).headerTextViewSign
                signTextView.text = it
            })
             getAllWords().observe(this@WordsActivity, Observer {
                 navigationView.menu.findItem(R.id.menu_count).title = "单词个数: ${it.size}"
             })
        }

    }
}